package com.dongbo.recorder.screenrecord.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.just.library.AgentWeb;
import com.just.library.ChromeClientCallbackManager;
import com.dongbo.recorder.screenrecord.R;
import com.dongbo.recorder.screenrecord.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

//import com.just.library.AgentWeb;
//import com.just.library.ChromeClientCallbackManager;

/**
 * @author ltx
 * @date :2020/5/7 15:27
 * @description: web加载页面
 */
public class WebActivity extends BaseActivity {

    public static final String EXTRA_KEY_TITLE = "EXTRA_KEY_TITLE";
    public static final String EXTRA_KEY_URL = "EXTRA_KEY_URL";

    @BindView(R.id.vip_back)
    ImageView vipBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_web_container)
    LinearLayout llWebContainer;

    private String mTitle = "";
    private String mUrl;
    private AgentWeb mAgentWeb;

    public static void start(Context context, String title, String url) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(EXTRA_KEY_TITLE, title);
        intent.putExtra(EXTRA_KEY_URL, url);
        context.startActivity(intent);
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_web;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initData() {
        mTitle = getIntent().getStringExtra(EXTRA_KEY_TITLE);
        mUrl = getIntent().getStringExtra(EXTRA_KEY_URL);
        tvTitle.setText(mTitle);
        initWeb();
        vipBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initWeb() {
        mAgentWeb = AgentWeb.with(this)//
                .setAgentWebParent(llWebContainer, new LinearLayout.LayoutParams(-1, -1))//
                .useDefaultIndicator()//
                .defaultProgressBarColor()
                .setReceivedTitleCallback(mCallback)
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
//                .composeWebViewClientBase(new SonicWebViewClient()) //测试 WebViewClient 中间件
//                .composeWebViewClientBase(new SonicWebViewClient())//测试 WebViewClient 中间件
//                .composeWebViewClientBase(new SonicWebViewClient())//测试 WebViewClient 中间件
//                .composeWebChromeClientBase(new MiddleWareChromeClient())//测试 WareChromeClient 中间件
                //.setMainFrameErrorView(R.layout.agentweb_error_page,-1)
                .setSecutityType(AgentWeb.SecurityType.strict)
//                    .setSecurityType(AgentWeb.SecurityType.strict)
                // .setWebLayout(new WebLayout(this))
                .openParallelDownload()//打开并行下载 , 默认串行下载
//                    .setNotifyIcon(R.drawable.ic_file_download_black_24dp) //下载图标
                .setNotifyIcon(R.mipmap.download) //下载图标
                //.setOpenOtherAppWays(DefaultWebClient.OpenOtherAppWays.ASK)//打开其他应用时，弹窗质询用户前往其他应用
                //.interceptUnkownScheme() //拦截找不到相关页面的Scheme
                .createAgentWeb()//
                .ready()
                .go(getUrl());
    }


    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            //firstUrl = request.getUrl().toString();
            return super.shouldOverrideUrlLoading(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    };
    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
        }
    };

    public String getUrl() {
        return mUrl;
    }

    private ChromeClientCallbackManager.ReceivedTitleCallback mCallback = new ChromeClientCallbackManager.ReceivedTitleCallback() {

        @Override
        public void onReceivedTitle(WebView view, String title) {

        }
    };

    @Override
    protected void initListener() {

    }


    @OnClick(R.id.vip_back)
    public void onViewClicked() {
        finish();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mAgentWeb.uploadFileResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mAgentWeb.destroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }

}
